<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class pengguna extends INSP_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('ctrlpnl/model_admin');
	}
	
	function index() {
		if ($this->session->userdata('logged_in') == TRUE) {
			$data['base_url'] = base_url();	
			$data['module']	 = 'pengguna-admin';
			$this->load->view('ctrlpnl/vmain',$data);
		} else {
			$baseUrl = base_url();
			redirect($baseUrl.'ctrlpnl-insp-jkt-ci3500-admin-area51');
		}
	}
	
	function formdata($param=""){
		$data['editstatus'] 	= $param;
		$data['base_url'] = base_url();	
		if($param == "new"){
			$data['stateid'] 		= "";
			$data['username'] 		= "";
			$data['password'] 		= "";
			$data['fullname']		= "";
			$data['phone']			= "";
			$data['picture']		= "";
			$data['accessible']		= "";
			$data['active_status']	= "";
		}elseif($param == "edit"){
			$id = $this->input->post('ID');
			$baseurl = base_url();
			$query = $this->model_admin->getRecord('tb_user_account', 'stateid', $id);
			
			$data['stateid'] 		= $query->stateid;
			$data['username'] 		= $query->username;
			$data['password'] 		= $query->password;
			$data['fullname']		= $query->fullname;
			$data['phone']			= $query->phone;
			$data['picture']		= $query->picture;
			$data['accessible']		= $query->accessible;
			$data['active_status']	= $query->active_status;
				
		}
		
		$this->load->view('ctrlpnl/pengguna-admin/form-pengguna', $data);
	}
	
	function dataPengguna(){
		echo $this->model_admin->getData('tb_user_account');
	}
	
	function saveData($param=""){
		$post = array();
		foreach($_POST as $k=>$v) $post[$k] = $this->input->post($k);
		if($param=="new"){
			$this->load->library('encrypt');
			$username = $post['username'];
			if($post['accessible'] == 0) $password = "user123";
			else $password = "admin123";
			
			$post['password'] = $this->encrypt->encode($username.$password);
		}
		echo $this->model_admin->saveDataModel($post,'tb_user_account',$param);

	}
	
	function combo($param=""){
		$optTemp = "";
		$p2 = $this->input->post("v");
		if($param=="status_pengguna"){
			$data = array();
			$data[] = array(
				'code' => 'Y',
				'status' => 'Pengguna Aktif'
			);
			$data[] = array(
				'code' => 'N',
				'status' => 'Pengguna Tidak Aktif'
			);
		}elseif($param=="hak_akses"){
			$data = array();
			$data[] = array(
				'code' => '1',
				'status' => 'Administrator'
			);
			$data[] = array(
				'code' => '0',
				'status' => 'Penulis Aktif'
			);
		}
		
		$optTemp .= "<option value=''>-- Pilih --</option>";			
		foreach($data as $v=>$k)
		{
			if($p2 == $k['code'])
				$optTemp .= "<option selected value='".$k['code']."'>".$k['status']."</option>";
			else 
				$optTemp .= "<option value='".$k['code']."'>".$k['status']."</option>";
		}
		echo $optTemp;	

	}
	
	function ubahpassword($parameter=""){
		if($parameter == 'form'){
			$data['base_url'] = base_url();
			$this->load->view('ctrlpnl/pengguna-admin/ubah_password',$data);
		}elseif($parameter == "savepassword"){
			$username 		= $this->session->userdata('username');
			$ubah_password = $this->input->post('new_password');
			$id 		   = $this->input->post('stateid');
			$this->load->library('encrypt');
			$password_baru = $this->encrypt->encode($username.$ubah_password);
			
			echo $this->db->update('tb_user_account',array('password'=>$password_baru),array('stateid'=>$id));
		}
	}
}